/****************************************************************
 * Fulguro
 * Copyright (C) 2004 Christophe Clienti
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 ***************************************************************/


#include <string.h>
#include "flgrCoreData.h"
#include "flgrCoreDispatch.h"
#include "flgrCoreIO.h"
#include "flgrCoreSampling.h"

/*!
 * \ingroup group_fulguro_core_copy
 * @{
 */



 /************************************* 1D Functions ***************************************/

#define FLGR_MACRO_DOWNSAMPLE_1D(dtype)			\
  dtype *dest = (dtype*) datdest->array;		\
  dtype *src  = (dtype*) datsrc->array;			\
  dtype val;						\
  int i,k,l,m,x;					\
  int spp = datsrc->spp;				\
  int length = 	datsrc->length*spp;			\
							\
  for(i=0,m=0 ; i<length ; i+=factor*spp,m+=spp) {	\
    for(x=0,k=i,l=m ; x<datsrc->spp ; x++,k++,l++) {	\
      val = flgr_get_array_##dtype(src,k);		\
      flgr_set_array_##dtype(dest,l,val);		\
    }							\
  }							\
							\
  return



void flgr1d_downsample_fgBIT(FLGR_Data1D *datdest, FLGR_Data1D *datsrc, int factor) {
  FLGR_MACRO_DOWNSAMPLE_1D(fgBIT);
}
void flgr1d_downsample_fgUINT8(FLGR_Data1D *datdest, FLGR_Data1D *datsrc, int factor) {
  FLGR_MACRO_DOWNSAMPLE_1D(fgUINT8);
}
void flgr1d_downsample_fgUINT16(FLGR_Data1D *datdest, FLGR_Data1D *datsrc, int factor) {
  FLGR_MACRO_DOWNSAMPLE_1D(fgUINT16);
}
void flgr1d_downsample_fgUINT32(FLGR_Data1D *datdest, FLGR_Data1D *datsrc, int factor) {
  FLGR_MACRO_DOWNSAMPLE_1D(fgUINT32);
}
void flgr1d_downsample_fgINT8(FLGR_Data1D *datdest, FLGR_Data1D *datsrc, int factor) {
  FLGR_MACRO_DOWNSAMPLE_1D(fgINT8);
}
void flgr1d_downsample_fgINT16(FLGR_Data1D *datdest, FLGR_Data1D *datsrc, int factor) {
  FLGR_MACRO_DOWNSAMPLE_1D(fgINT16);
}
void flgr1d_downsample_fgINT32(FLGR_Data1D *datdest, FLGR_Data1D *datsrc, int factor) {
  FLGR_MACRO_DOWNSAMPLE_1D(fgINT32);
}
void flgr1d_downsample_fgFLOAT32(FLGR_Data1D *datdest, FLGR_Data1D *datsrc, int factor) {
  FLGR_MACRO_DOWNSAMPLE_1D(fgFLOAT32);
}
void flgr1d_downsample_fgFLOAT64(FLGR_Data1D *datdest, FLGR_Data1D *datsrc, int factor) {
  FLGR_MACRO_DOWNSAMPLE_1D(fgFLOAT64);
}


//////////////////////////////////////////////////////////////////
/*! Downsample a data1d
 *  @param datdest : a pointer to a FLGR_Data1D
 *  @param datsrc  : a pointer to a FLGR_Data1D
 *  @param factor  : downsampling factor
 *  @returns FLGR_RET_OK, ...
 */
//////////////////////////////////////////////////////////////////
FLGR_Ret flgr1d_downsample(FLGR_Data1D *datdest, FLGR_Data1D *datsrc, int factor) {
  FLGR_Ret ret;

  FLGR_DECLARE_FUNCTION;

  if((datdest==NULL) || (datsrc==NULL)) {
    POST_ERROR("Null objects!\n");
    return FLGR_RET_NULL_OBJECT;
  }
  
  if( (ret=flgr1d_is_data_same_type(datsrc,datdest)) != FLGR_RET_OK ) {
    POST_ERROR("src and destination have different or illegal types");
    return ret;
  }

  if( (ret=flgr1d_is_data_same_spp(datsrc,datdest)) != FLGR_RET_OK ) {
    POST_ERROR("src and destination have different sample per pixels");
    return ret;
  }

  if( ((datsrc->length)/factor) != datdest->length ) {
    POST_ERROR("src does not fit in dest\n");
    return FLGR_RET_SIZE_ERROR;
  }

  FLGR_DISPATCH_PROCEDURE(datsrc->type,flgr1d_downsample,datdest,datsrc,factor);
}


 /************************************* 2D Functions ***************************************/





#define FLGR_MACRO_DOWNSAMPLE_2D(dtype)			\
  int i;						\
  FLGR_Data1D **dest = datdest->row;			\
  FLGR_Data1D **src  = datsrc->row;			\
							\
  							\
  for(i=0 ; i<datsrc->size_y ; i+=factor_y) {		\
    flgr1d_downsample_##dtype(*dest,*src,factor_x);	\
    dest++; src+=factor_y;				\
  }							\
							\
  return



void flgr2d_downsample_fgBIT(FLGR_Data2D *datdest, FLGR_Data2D *datsrc, int factor_x, int factor_y) {
  FLGR_MACRO_DOWNSAMPLE_2D(fgBIT);
}
void flgr2d_downsample_fgUINT8(FLGR_Data2D *datdest, FLGR_Data2D *datsrc, int factor_x, int factor_y) {
  FLGR_MACRO_DOWNSAMPLE_2D(fgUINT8);
}
void flgr2d_downsample_fgUINT16(FLGR_Data2D *datdest, FLGR_Data2D *datsrc, int factor_x, int factor_y) {
  FLGR_MACRO_DOWNSAMPLE_2D(fgUINT16);
}
void flgr2d_downsample_fgUINT32(FLGR_Data2D *datdest, FLGR_Data2D *datsrc, int factor_x, int factor_y) {
  FLGR_MACRO_DOWNSAMPLE_2D(fgUINT32);
}
void flgr2d_downsample_fgINT8(FLGR_Data2D *datdest, FLGR_Data2D *datsrc, int factor_x, int factor_y) {
  FLGR_MACRO_DOWNSAMPLE_2D(fgINT8);
}
void flgr2d_downsample_fgINT16(FLGR_Data2D *datdest, FLGR_Data2D *datsrc, int factor_x, int factor_y) {
  FLGR_MACRO_DOWNSAMPLE_2D(fgINT16);
}
void flgr2d_downsample_fgINT32(FLGR_Data2D *datdest, FLGR_Data2D *datsrc, int factor_x, int factor_y) {
  FLGR_MACRO_DOWNSAMPLE_2D(fgINT32);
}
void flgr2d_downsample_fgFLOAT32(FLGR_Data2D *datdest, FLGR_Data2D *datsrc, int factor_x, int factor_y) {
  FLGR_MACRO_DOWNSAMPLE_2D(fgFLOAT32);
}
void flgr2d_downsample_fgFLOAT64(FLGR_Data2D *datdest, FLGR_Data2D *datsrc, int factor_x, int factor_y) {
  FLGR_MACRO_DOWNSAMPLE_2D(fgFLOAT64);
}


//////////////////////////////////////////////////////////////////
/*! Downsample a data2d
 *  @param datdest : a pointer to a FLGR_Data2D
 *  @param datsrc  : a pointer to a FLGR_Data2D
 *  @param factor_x  : downsampling factor X
 *  @param factor_y  : downsampling factor Y
 *  @returns FLGR_RET_OK, ...
 */
//////////////////////////////////////////////////////////////////
FLGR_Ret flgr2d_downsample(FLGR_Data2D *datdest, FLGR_Data2D *datsrc, int factor_x, int factor_y) {
  FLGR_Ret ret;

  FLGR_DECLARE_FUNCTION;

  if((datdest==NULL) || (datsrc==NULL)) {
    POST_ERROR("Null objects!\n");
    return FLGR_RET_NULL_OBJECT;
  }
  
  if( (ret=flgr2d_is_data_same_type(datsrc,datdest)) != FLGR_RET_OK ) {
    POST_ERROR("src and destination have different or illegal types");
    return ret;
  }

  if( (ret=flgr2d_is_data_same_spp(datsrc,datdest)) != FLGR_RET_OK ) {
    POST_ERROR("src and destination have different sample per pixels");
    return ret;
  }

  if( ((datsrc->size_x)/factor_x) != datdest->size_x ) {
    POST_ERROR("src does not fit in dest\n");
    return FLGR_RET_SIZE_ERROR;
  }

  if( ((datsrc->size_y)/factor_y) != datdest->size_y ) {
    POST_ERROR("src does not fit in dest\n");
    return FLGR_RET_SIZE_ERROR;
  }

  FLGR_DISPATCH_PROCEDURE(datsrc->type,flgr2d_downsample,datdest,datsrc,factor_x,factor_y);
}















//!@}





